package com.lyf.boot.modules.index.controller;

import com.lyf.boot.core.model.R;
import com.lyf.boot.handler.KafkaSendResultHandler;
import jakarta.annotation.PostConstruct;
import lombok.RequiredArgsConstructor;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * kafka
 * <a href="https://blog.csdn.net/friggly/article/details/129158125">参考</a>
 * @author Luyufan
 * @date 2024/10/16
 **/
@RestController
@RequestMapping("/kafka")
@RequiredArgsConstructor
public class KafkaController {

    private final KafkaTemplate<Object, Object> kafkaTemplate;

    private final KafkaSendResultHandler kafkaSendResultHandler;

    @PostConstruct
    public void init(){
        kafkaTemplate.setProducerListener(kafkaSendResultHandler);
    }

    @GetMapping("/send")
    @Transactional
    public R<Void> send(String message){
        kafkaTemplate.send("topic1", message);
        return R.ok();
    }
}
